<template>
  <view class="index" :style="{ overflow: 'hidden', height: pageHeight }">
    <!-- 搜索 -->
    <Search @search="search" />
    <!-- 轮播图 -->
    <view class="slider">
      <swiper
        autoplay
        interval="2000"
        circular
        indicator-dots
        indicator-color="rgba(255,255,255,1)"
        indicator-active-color="rgba(255,255,255,.6)"
      >
        <swiper-item v-for="item in swipers" :key="item.goods_id">
          <!-- 跳转详情页，携带商品ID -->
          <navigator :url="`/packone/goods/index?id=${item.goods_id}`">
            <image :src="item.image_src" />
          </navigator>
        </swiper-item>
      </swiper>
    </view>
    <!-- 功能导航 -->
    <view class="navs">
      <navigator
        :open-type="item.navigator_url ? 'switchTab' : 'navigate'"
        :url="
          item.navigator_url
            ? '/pages/category/index'
            : '/packone/list/index?query=' + item.name
        "
        v-for="item in navs"
        :key="item.name"
      >
        <image :src="item.image_src" />
      </navigator>
    </view>
    <!-- 栏目楼层 -->
    <view class="floors">
      <view class="floor" v-for="(item, i) in floors" :key="i">
        <!-- title -->
        <view class="ftitle">
          <image :src="item.floor_title.image_src" />
        </view>
        <!-- pics -->
        <view class="fitem">
          <navigator
            :url="'/packone/list/index?query=' + prd.name"
            v-for="prd in item.product_list"
            :key="prd.name"
          >
            <image :src="prd.image_src" />
          </navigator>
        </view>
      </view>
    </view>
    <!-- 底部提示 -->
    <view class="end">
      <text>我是有底线的！</text>
    </view>
    <!-- 回到顶部 -->
    <view v-if="showTop" @click="goTop" class="goTop icon-top"></view>
  </view>
</template>

<script>
// import Search from '@/components/search'
// import { request, request2 } from '@/utils/request.js'
export default {
  // components: {
  //   Search,
  // },
  data () {
    return {
      showTop: false, // 是否显示回顶按钮
      pageHeight: 'auto',
      swipers: [], // 轮播图数据
      navs: [], // 导航
      floors: [] // 商品楼层数据
    }
  },
  async onLoad () {
    this.getSwiper()
    this.getNav()
    this.getFloor()
  },
  // 用户下拉刷新执行
  onPullDownRefresh () {
    console.log('下拉刷新了...')
    // 下拉刷新=》重新获取最新的商品数据
    // this.getSwiper()
    // this.getNav()
    // this.getFloor()
    // 需求：三个请求都成功了，关闭下拉刷新=》Promise.all([异步请求1, 异步请求2...])
    Promise.all([this.getSwiper(), this.getNav(), this.getFloor()]).then(() => {
      // 都成功了，走到这里=》关闭下拉刷新
      uni.stopPullDownRefresh()
    }).catch(() => {
      // 有一个失败就走到这里
      uni.showToast({
        title: '刷新失败！'
      })
      uni.stopPullDownRefresh()
    })
  },
  // 需求：页面滚动到可视区域一半的时候，显示回顶按钮
  // 页面滚动执行
  onPageScroll (e) {
    // console.log('距离顶部高度：', e.scrollTop)
    if (e.scrollTop > uni.getSystemInfoSync().windowHeight / 2) {
      // 滚动高度大于一半显示按钮
      this.showTop = true
    } else {
      // 不满足条件不显示
      this.showTop = false
    }

  },
  methods: {

    goTop () {
      uni.pageScrollTo({
        scrollTop: 0, // 给0，代表距离顶部为0px
        duration: 300
      });
    },
    // 获取轮播图
    async getSwiper () {
      const res = await this.request({
        url: '/api/public/v1/home/swiperdata',
        method: 'get'
      })
      console.log('轮播图：', res.data)
      this.swipers = res.data
    },
    // 获取导航
    async getNav () {
      const res = await this.request({
        url: '/api/public/v1/home/catitems'
      })
      console.log('导航：', res.data)
      this.navs = res.data
    },
    // 获取楼层
    async getFloor () {
      const res = await this.request({
        url: '/api/public/v1/home/floordata'
      })
      console.log('楼层：', res.data)
      this.floors = res.data
    },
    // 子传父回调函数
    search (data) {
      console.log('可见区域高度：', data)
      // 存储到data
      this.pageHeight = data
    }
  }
};
</script>

<style lang="scss">
// 首页
.index {
  // 轮播
  .slider {
    swiper,
    image {
      width: 750rpx;
      height: 340rpx;
    }
  }
  // 功能导航
  .navs {
    display: flex;
    padding: 30rpx;
    background: #fff;
    justify-content: space-between;
    navigator {
      width: 128rpx;
      height: 140rpx;
    }
  }

  // 栏目楼层
  .floor {
    .ftitle {
      padding-top: 30rpx;
      background: #f4f4f4;
      image {
        width: 750rpx;
        height: 60rpx;
      }
    }
    // pics
    .fitem {
      padding: 20rpx 16rpx 10rpx;
      overflow: hidden;
      navigator {
        float: left;
        width: 193rpx;
        height: 188rpx;
        margin-left: 10rpx;
        margin-bottom: 10rpx;
      }
      navigator:nth-child(1) {
        width: 232rpx;
        height: 386rpx;
        margin-left: 0;
      }
      navigator:nth-child(2),
      navigator:nth-child(5) {
        width: 273rpx;
        height: 188rpx;
      }
    }
    &:first-child {
      .fitem {
        navigator {
          width: 233rpx;
        }
      }
    }
  }
  .end {
    text-align: center;
    font-size: 24rpx;
    color: #999;
  }
  .goTop {
    position: fixed;
    bottom: 30rpx;
    /* #ifdef H5 */
    bottom: 65px;
    /* #endif */
    right: 30rpx;

    display: flex;
    justify-content: center;
    align-items: center;
    width: 100rpx;
    height: 100rpx;
    font-size: 48rpx;
    color: #666;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.8);
  }
}
</style>
